package szwolaiyun.paireach.com.szwolaizhuang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.paireach.szwolaiyun.sz_common_widget.activity.BaseActivity;

import szwolaiyun.paireach.com.szwolaizhuang.R;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.DriveOrder;
import szwolaiyun.paireach.com.szwolaizhuang.model.business.OperatorStatus;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.BaseEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;
import szwolaiyun.paireach.com.szwolaizhuang.net.json.JsonPaser;

public class ScanResultActivity extends BaseActivity implements View.OnClickListener {

    private String scanResult;
    private ProgressBar progressBar;
    private TextView txt_error;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_result);
        scanResult = getIntent().getStringExtra("result");
        findViewById(R.id.layout_top_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.layout_top_change)).setText("扫描结果");
        txt_error = (TextView) findViewById(R.id.txt_error);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        progressBar.setVisibility(View.VISIBLE);
        txt_error.setVisibility(View.GONE);
        getOrderDetail(scanResult);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.layout_top_back) {
            finish();
        }
    }

    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if (event.getRequestTag() == RequestTag.GET_ORDER_DETAIL_BY_ORDER_NO) {
            //要获取一个运单详情，取出运单状态。
            //如果不是指定的状态下直接提示错误，不要进行下一步操作
            Log.d("网络获取成功", "解析成功数据-获取运单 详情");
            JsonPaser.parseDriverOrderDetailByOrderNumber(event.getJson());
        }
    }

    @Override
    public void httpErrorEvent(HttpErrorEvent errorEvent) {
        super.httpErrorEvent(errorEvent);
        if (errorEvent.getRequestTag() == RequestTag.GET_ORDER_DETAIL_BY_ORDER_NO) {
            progressBar.setVisibility(View.GONE);
            txt_error.setVisibility(View.VISIBLE);
            txt_error.setText("错误：" + errorEvent.getErrorMessage());
            showToast("错误：" + errorEvent.getErrorMessage());
        }
    }

    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
        if (event.getTag().equals("get_order_detail_info_by_no")) {
            Log.d("ScanResultActivity-JSON", "解析app数据-根据单号获取运单详情");
            if (event.getCode() == 0) {
                DriveOrder d = (DriveOrder) event.getObj1();
                if (d.getOrderStateCode() == 226 ||
                        d.getOrderStateCode() == 238 || d.getOrderStateCode() == 228 ||
                        d.getOrderStateCode() == 240) {
                    //可以扫码，跳转到选择叉车工的界面
                    Intent intent=new Intent(this, SelectFriendActivity.class);
                    if(d.getOrderStateCode() == 226 ||
                            d.getOrderStateCode() == 238){
                        intent.putExtra("workType","load");
                    } else {
                        intent.putExtra("workType","unload");
                    }
                    intent.putExtra("no", scanResult);
                    startActivity(intent);


                    finish();
                } else {
                    progressBar.setVisibility(View.GONE);
                    txt_error.setVisibility(View.VISIBLE);
                    txt_error.setText("还没轮到你装货呢");
                }
            } else {
                progressBar.setVisibility(View.GONE);
                txt_error.setVisibility(View.VISIBLE);
                txt_error.setText(event.getExtraInfo());
            }
        }
    }

    private void getOrderDetail(String orderNo) {
        new WayBillRequest().getOrderDetailByOrderNumber(orderNo);
    }
}
